package com.time.timespace.handler;

import com.alibaba.fastjson.JSON;
import com.time.timespace.commons.dto.ResponseEntity;
import com.time.timespace.commons.utils.WebUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {


    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        //返回给前端的json
        ResponseEntity<Object> responseResult = ResponseEntity.builder()
                .timestamp(System.currentTimeMillis())
                .status(String.valueOf(HttpStatus.FORBIDDEN.value()))
                .message("您的权限不足!")
                .data(null).build();
        String json = JSON.toJSONString(responseResult);
        WebUtils.renderString(response, json);
    }
}
